home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Development Kits / MPW etc / MPW-GM / MPW / Examples / CFMExamples / ModApp / AppleEventHandlers.c next >
Encoding:
C/C++ Source or Header  |  1998-12-03  |  3.5 KB  |  127 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        AppleEventHandlers.c
  3.  
  4.     Contains:    Handlers for the 4 "required" events
  5.                 (although this app doesn't have documents to print)
  6.  
  7.     Written by:    Richard Clark
  8.  
  9.     Copyright:    © 1993 by Apple Computer, Inc., all rights reserved.
  10.  
  11.     Change History (most recent first):
  12.  
  13.                   11/28/93    RC        First release
  14.  
  15.     To Do:
  16. */
  17.  
  18.  
  19. #ifndef __MIXEDMODE__
  20.     #include <MixedMode.h>
  21. #endif
  22.  
  23. #ifndef __GESTALT__
  24.     #include <Gestalt.h>
  25. #endif
  26.  
  27. #ifndef __APPLEEVENTS__
  28.     #include <AppleEvents.h>
  29. #endif
  30.  
  31. #ifndef __ERRORS__
  32.     #include <Errors.h>
  33. #endif
  34.  
  35. #include "ModApp.h"
  36. #include "Prototypes.h"
  37.  
  38. static pascal OSErr HandleOapp (AEDescList *aevt, AEDescList *reply, long refCon);
  39. static pascal OSErr HandleQuit (AEDescList *aevt, AEDescList *reply, long refCon);
  40. static pascal OSErr HandleOdoc (AEDescList *aevt, AEDescList *reply, long refCon);
  41. static pascal OSErr HandlePdoc (AEDescList *aevt, AEDescList *reply, long refCon);
  42.  
  43. #ifndef NewEventHandlerProc
  44.     #define NewEventHandlerProc(x) (EventHandlerProcPtr)x
  45. #endif
  46.  
  47. void InstallAppleEventHandlers()
  48. {
  49.     OSErr    err;
  50.     long    result;
  51.  
  52.     err = Gestalt(gestaltAppleEventsAttr, &result);
  53.     if (err == noErr) {    
  54.         (void)AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, NewAEEventHandlerProc(HandleOapp), 0, false);
  55.         (void)AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,   NewAEEventHandlerProc(HandleOdoc), 0, false);
  56.         (void)AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,  NewAEEventHandlerProc(HandlePdoc), 0, false);
  57.         (void)AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, NewAEEventHandlerProc(HandleQuit), 0, false);
  58.     }
  59. } /* init_apple_events_hook */
  60.  
  61.  
  62. pascal OSErr HandleOapp (AEDescList *aevt, AEDescList *reply, long refCon)
  63. {
  64. #pragma unused (aevt, reply, refCon)
  65.     NewDisplayWindow();
  66.     return noErr;
  67. } /* NotHandled */
  68.  
  69.  
  70. pascal OSErr HandleOdoc (AEDescList *aevt, AEDescList *reply, long refCon)
  71. // We'll get this if somebody double-clicks on a tool, so create a window and
  72. // load up a tool
  73. {
  74.     #pragma unused (reply, refCon)
  75.     AEDesc        fileListDesc = {'NULL', NULL};
  76.     long        numFiles;
  77.     DescType    actualType;
  78.     long        actualSize;
  79.     AEKeyword    actualKeyword;
  80.     FSSpec        oneFile;
  81.     long        index;
  82.     OSErr        err;
  83.                         
  84.     /* The "odoc" and "pdoc" messages contain a list of aliases as the direct paramater.  */
  85.     /* This means that we'll need to extract the list, count the list's elements, and     */
  86.     /* then process each file in turn.                                                                                                         */
  87.     
  88.     /* Extract the list of aliases into fileListDesc */
  89.     err = AEGetKeyDesc( aevt, keyDirectObject, typeAEList, &fileListDesc );
  90.     if (err) goto done;
  91.         
  92.     /* Count the list elements */
  93.     err = AECountItems( &fileListDesc, &numFiles);
  94.     if (err) goto done;
  95.                 
  96.     /* now get each file from the list and process it. */
  97.     /* Even though the event contains a list of alises, the Apple Event Manager */
  98.     /* will convert each alias to an FSSpec if we ask it to. */
  99.     for (index = 1; index <= numFiles; index ++) {
  100.         err = AEGetNthPtr( &fileListDesc, index, typeFSS, &actualKeyword,
  101.                             &actualType, (Ptr)&oneFile, sizeof(oneFile), &actualSize);
  102.         if (err) goto done;
  103.         
  104.         NewDisplayWindow();
  105.         LoadTool(&oneFile, FrontWindow());
  106.     }
  107.  
  108. done:
  109.     (void)AEDisposeDesc(&fileListDesc);
  110.     return err;
  111. } /* HandleOdoc */
  112.  
  113.  
  114. pascal OSErr HandlePdoc (AEDescList *aevt, AEDescList *reply, long refCon)
  115. {
  116. #pragma unused (aevt, reply, refCon)
  117.     return errAEEventNotHandled;
  118. } /* HandlePdoc */
  119.  
  120.  
  121. pascal OSErr HandleQuit (AEDescList *aevt, AEDescList *reply, long refCon)
  122. {
  123. #pragma unused (aevt, reply, refCon)
  124.     gDone = true;
  125.     return noErr;
  126. } /* HandleQuit */
  127.